﻿namespace MadMediaManager.Core.Domain.Components
{
    public abstract class UnitValue<TUnitValue, TUnit> where TUnitValue : UnitValue<TUnitValue, TUnit>
    {
        public TUnit Unit { get; set; }
        public double? Value { get; set; }

        /// <summary>
        /// Converts the value of this instance to the specified unit and returns a new instance of <see cref="UnitValue{TUnitValue, TUnit}"/>.
        /// </summary>
        /// <param name="toUnit">The unit to which this <see cref="Value"/> will be converted</param>
        /// <returns>A new instance housing the converted information</returns>
        public abstract TUnitValue ConvertTo(TUnit toUnit);

        /// <summary>
        /// Converts this instance to the specifed unit
        /// </summary>
        /// <param name="toUnit">The unit to which this object will be converted</param>
        public virtual void ConvertThisTo(TUnit toUnit)
        {
            var newUnitValue = ConvertTo(toUnit);
            Unit = newUnitValue.Unit;
            Value = newUnitValue.Value;
        }
    }
}
